package com.lefeng.cockpit.datax.conf;

import com.lefeng.cockpit.datax.dao.*;
import com.lefeng.cockpit.datax.dao.mapper.JobInfoMapper;
import com.lefeng.cockpit.datax.dao.mapper.JobLogMapper;
import com.lefeng.cockpit.datax.scheduler.JobScheduler;
import lombok.Data;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;

import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.sql.DataSource;

/**
 * xxl-job config
 *
 * @author xuxueli 2017-04-28
 */

@Component
@Data
public class JobAdminConfig implements InitializingBean, DisposableBean {

    private static JobAdminConfig adminConfig = null;

    public static JobAdminConfig getAdminConfig() {
        return adminConfig;
    }


    // ---------------------- XxlJobScheduler ----------------------

    private JobScheduler xxlJobScheduler;

    @Override
    public void afterPropertiesSet() throws Exception {
        adminConfig = this;

        xxlJobScheduler = new JobScheduler();
        xxlJobScheduler.init();
    }

    @Override
    public void destroy() throws Exception {
        xxlJobScheduler.destroy();
    }


    // ---------------------- XxlJobScheduler ----------------------

    // conf
    @Value("${datax.job.i18n:}")
    private String i18n;

    @Value("${datax.job.accessToken:}")
    private String accessToken;

    @Value("${spring.mail.username:}")
    private String emailUserName;

    @Value("${datax.job.triggerpool.fast.max:200}")
    private int triggerPoolFastMax;

    @Value("${datax.job.triggerpool.slow.max:100}")
    private int triggerPoolSlowMax;

    @Value("${datax.job.logretentiondays:30}")
    private int logretentiondays;

    @Value("${datasource.aes.key:AD42F6697B035B75}")
    private String dataSourceAESKey;

    // dao, service

    @Resource
    private JobLogDao jobLogDao;

    @Resource
    private JobInfoDao jobInfoDao;

    @Resource
    private JobRegistryDao jobRegistryDao;

    @Resource
    private ExecutorDao executorDao;

    @Resource
    private JobInfoReportDao jobInfoReportDao;

    @Resource
    private DataSource dataSource;

//    @ResourcePO
//    private JobLogReportMapper jobLogReportMapper;
////    @ResourcePO
////    private JavaMailSender mailSender;
    @Resource
    private DataSourceDao dataSourceDao;
//    @ResourcePO
//    private JobDatasourceMapper jobDatasourceMapper;
//
//    public String getI18n() {
//        return i18n;
//    }
//
//    public String getAccessToken() {
//        return accessToken;
//    }
//
//    public String getEmailUserName() {
//        return emailUserName;
//    }
//
//    public int getTriggerPoolFastMax() {
//        return triggerPoolFastMax < 200 ? 200 : triggerPoolFastMax;
//    }
//
//    public int getTriggerPoolSlowMax() {
//        return triggerPoolSlowMax < 100 ? 100 : triggerPoolSlowMax;
//    }
//
//    public int getLogretentiondays() {
//        return logretentiondays < 7 ? -1 : logretentiondays;
//    }
//
//    public JobLogMapper getJobLogMapper() {
//        return jobLogMapper;
//    }
//
//    public JobInfoMapper getJobInfoMapper() {
//        return jobInfoMapper;
//    }
//
//    public JobRegistryMapper getJobRegistryMapper() {
//        return jobRegistryMapper;
//    }
//
//    public JobGroupMapper getJobGroupMapper() {
//        return jobGroupMapper;
//    }
//
//    public JobLogReportMapper getJobLogReportMapper() {
//        return jobLogReportMapper;
//    }
//
//    public JavaMailSender getMailSender() {
//        return mailSender;
//    }
//
//    public DataSource getDataSource() {
//        return dataSource;
//    }
//
//    public JobDatasourceMapper getJobDatasourceMapper() {
//        return jobDatasourceMapper;
//    }
//
//    public String getDataSourceAESKey() {
//        return dataSourceAESKey;
//    }
//
//    public void setDataSourceAESKey(String dataSourceAESKey) {
//        this.dataSourceAESKey = dataSourceAESKey;
//    }
}
